package com.zhengsr.wanandroid.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhengsr.corelib.bean.KnowTreeBean;
import com.zhengsr.wanandroid.R;
import com.zhengsr.wanandroid.activity.KnowledgeDetaileActivity;
import com.zhengsr.wanandroid.mvp.base.BaseFragment;
import com.zhengsr.wanandroid.mvp.base.BasePresent;
import com.zhengsr.wanandroid.mvp.contract.ContractManager;
import com.zhengsr.wanandroid.mvp.present.KnowledgePresent;
import com.zhengsr.wanandroid.ui.main.adapter.KnowTreeAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhengshaorui
 * Time on 2019/2/25
 */

public class SystemKnowFragment extends BaseFragment implements ContractManager.IKnowTree, BaseQuickAdapter.OnItemClickListener {
    
    @BindView(R.id.et_search_view)
    TextView mTextView;
    @BindView(R.id.know_recyclerview)
    RecyclerView mRecyclerView;

    @BindView(R.id.normal_content)
    SmartRefreshLayout mSmartRefreshLayout;

    private KnowledgePresent mPresent;
    private KnowTreeAdapter mAdapter;
    private List<KnowTreeBean> mData = new ArrayList<>();

    public static SystemKnowFragment newInstance() {

        Bundle args = new Bundle();

        SystemKnowFragment fragment = new SystemKnowFragment();
        fragment.setArguments(args);
        return fragment;
    }
    
    @Override
    public int getLayoutId() {
        return R.layout.fragment_system_know;
    }

    @Override
    public BasePresent getPresent() {
        mPresent = KnowledgePresent.create(this);
        initRefreshlayout();
        return mPresent;
    }

    @Override
    public void reload() {
        super.reload();
        if (mPresent != null){
            mPresent.getAllKnowTree();
        }
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mAdapter = new KnowTreeAdapter(R.layout.item_know_tree,mData);
        LinearLayoutManager manager = new LinearLayoutManager(_mActivity,
                LinearLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void initView(View view) {
        mTextView.setText(R.string.know_system);

    }

    private void initRefreshlayout() {
        mSmartRefreshLayout.setEnableLoadMore(true);
        mSmartRefreshLayout.setEnableRefresh(true);
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mSmartRefreshLayout.finishRefresh(1000);
                mPresent.getAllKnowTree();
                Toast.makeText(_mActivity, R.string.lastest_data, Toast.LENGTH_SHORT).show();

            }
        });
        mSmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mSmartRefreshLayout.finishLoadMore(1000);
                mPresent.getAllKnowTree();
                Toast.makeText(_mActivity, R.string.lastest_data, Toast.LENGTH_SHORT).show();
            }
        });
    }



    @Override
    public void getAllTree(List<KnowTreeBean> knowTreeBeans) {
        mData.clear();
        mData.addAll(knowTreeBeans);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected boolean showNormalView() {
        return false;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
      //  Utils.startAricleActivity(view,_mActivity,null);
        KnowTreeBean bean = mData.get(position);
        Intent intent = new Intent(_mActivity,KnowledgeDetaileActivity.class);
        intent.putExtra("knowledge",bean);
        startActivity(intent);
    }
}
